package com.blockchain.console;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.P2PSystemDefine;
import com.blockchain.console.config.ConsoleConst;
import com.blockchain.console.service.common.LoginManage;
import com.blockchain.framework.http.servlet.Controller;
import com.blockchain.framework.http.servlet.PackageRewriter;
import com.blockchain.framework.http.servlet.Rewriter;
import com.blockchain.framework.http.session.SessionManager;
import com.blockchain.framework.http.session.authentication.AuthenticationException;
import com.blockchain.framework.http.session.authentication.PasswordAuthentication;
import com.blockchain.framework.service.ServiceSession;

public class ConsoleDefine extends P2PSystemDefine implements ConsoleConst {
	protected final Rewriter rewriter;

	public ConsoleDefine() {
		super();
		schemas.put(SessionManager.class, DB_CONSOLE_SESSION);
		rewriter = new PackageRewriter("com.blockchain.console.servlets");
	}

	@Override
	public String getGUID() {
		return "89ec2c73-e4fe-45b0-8bff-f87d63000079";
	}

	@Override
	public int readAccountId(ServiceSession serviceSession, String accountName,
			String password) throws AuthenticationException, SQLException {
		LoginManage loginManage = serviceSession.getService(LoginManage.class);
		return loginManage.readAccountId(accountName, password);
	}

	@Override
	public Rewriter getRewriter() {
		return rewriter;
	}

	@Override
	public boolean isStrictMVC() {
		return false;
	}

	@Override
	public void writeLog(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession,
			PasswordAuthentication authentication, int accountId) {
		LoginManage loginManage = serviceSession.getService(LoginManage.class);
		Controller controller = serviceSession.getController();
		String ip = controller.getRemoteAddr(request);
		try {
			loginManage.log(accountId,ip);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 开启多语言版本
	 * */
	@Override
	public boolean isMulitLanguage() {
		  return true;
	}
}
